import { _decorator, Component, Node, tween, Vec3 } from 'cc';
import { Main } from './Main';
import { Guest } from './Guest';
import { ResourceManager } from '../PlayableFramework/Tools/Manager/ResourceManager';
const { ccclass, property } = _decorator;

@ccclass('Belt')
export class Belt extends Component {
    
    @property(Main)
    public main:Main;


    private queue:Guest[]= [];
    protected onLoad(): void {
        this.main.onStep6 = ()=>{
            var queueParent = this.main.areaList[3].getChildByName("queue");
            for (let i = 0; i < queueParent.children.length; i++) {
                this.queue.push(queueParent.children[i].getComponent(Guest));
            }
            console.log("this.queue好了          "+this.queue.length);
        };
    }

    private index:number=0;
    giveBike(){

        const iii = this.index;
        this.index++;

        const pathRoot = ResourceManager.getPoolNode("Path4_1");
        const pathRoot2 = ResourceManager.getPoolNode("Path4_2");

        const biker = this.queue.shift();
        biker.node.setParent(this.main.areaList[3].getChildByName("running"));
        this.main.sortQueue(this.queue);

        biker.ride();

        biker.mover.setPathRoot(pathRoot);
        biker.mover.move();
        biker.anim.play("ride_bike")

        this.main.zhaji.play("zhaji");
        //抬杆
        tween(this.main.actor.node).delay(0.2).call(()=>{
            ResourceManager.spawn("MoneyEffect",this.main.zhaji.node);
            this.main.people--;
            this.main.moneyStack.gen(50* this.index,this.main.zhaji.node.worldPosition,()=>{
                this.main.money+=100;
            });
        }).start();
        
        biker.mover.onPathComplete = ()=>{
            //开始风火轮
            {
                const center1 = this.main.areaList[3].getChildByName("center"+(4 + iii));
                biker.node.setParent(center1,true);

                const rotateTime = 1.5;

                tween(center1).to(rotateTime,{eulerAngles:new Vec3(-360,-90,0)}).start();
                tween(biker.node).to(rotateTime,{position:new Vec3(4,-6.116,0)}).call(()=>{
                    biker.mover.setPathRoot(pathRoot2);
                    biker.mover.move();
                    biker.mover.onPathComplete = () =>{
                        console.log("减少一个人");
                    };
                }).start();
            }
        };
    }
}


